package logic

import (
	"context"

	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"

	"2106A-zg6/baidu/srv/internal/svc"
	"2106A-zg6/baidu/srv/models"
	"2106A-zg6/baidu/srv/pb/baidu"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserVipInfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserVipInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserVipInfoLogic {
	return &GetUserVipInfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetUserVipInfoLogic) GetUserVipInfo(in *baidu.GetUserVipInfoRequest) (*baidu.GetUserVipInfoResponse, error) {

	info, err := models.GetUserModelsVipInfo(in.UserId)
	if err != nil {
		return nil, status.Errorf(codes.InvalidArgument, err.Error())
	}

	var data []*baidu.Vip
	for _, v := range info {
		data = append(data, ModelRespVip(v))
	}

	return &baidu.GetUserVipInfoResponse{
		VipInfo: data,
	}, nil
}

func ModelRespVip(v models.Vip) *baidu.Vip {
	return &baidu.Vip{
		Id:     int64(v.ID),
		UserId: v.UserID,
		VipId:  v.VipId,
	}
}
